package com.cw.jeeyt.dao.impl.facade;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.cw.jeeyt.common.utils.StringUtil;
import com.cw.jeeyt.dao.intf.facade.FacadeClassDao;
import com.cw.jeeyt.dao.mapper.facade.FacadeClassMapper;
import com.cw.jeeyt.dao.model.facade.FacadeClassDo;
import com.cw.jeeyt.dao.model.facade.FacadeClassExample;
import com.cw.jeeyt.dao.model.facade.FacadeClassKeyDo;
import com.cw.jeeyt.dao.query.facade.FacadeClassQuery;
import com.cw.lang.common.utils.SequenceTool;
import com.cw.lang.common.dao.AbstractBaseDaoImpl;
import com.cw.lang.common.pagehelper.PageHelper;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 接口类数据库操作接口实现
 *
 * @author chenw
 * @date 2019-10-31 22:35:52
 */
@Repository
public class FacadeClassDaoImpl extends AbstractBaseDaoImpl<FacadeClassDo, FacadeClassMapper>
    implements FacadeClassDao {
    /**
     * 分页查询
     *
     * @param facadeClass 条件
     * @param pageParam 分页参数
     * @return
     */
    @Override
    public PageInfo<FacadeClassDo> selectPage(FacadeClassQuery facadeClass, PageParam pageParam) {
        FacadeClassExample example = buildExample(facadeClass);
        return PageHelper.<FacadeClassDo>startPage(pageParam.getPageNum(), pageParam.getPageSize()).doSelectPageInfo(
            () -> getMapper().selectByExample(example));
    }

    /**
     * 根据key查询接口类
     *
     * @param id
     * @return
     */
    @Override
    public FacadeClassDo selectByKey(Long id) {
        FacadeClassKeyDo facadeClassKeyDo = new FacadeClassKeyDo();
        facadeClassKeyDo.setId(id);
        return getMapper().selectByPrimaryKey(facadeClassKeyDo);
    }

    /**
     * 根据key删除接口类
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByKey(Long id) {
        FacadeClassKeyDo facadeClassKeyDo = new FacadeClassKeyDo();
        facadeClassKeyDo.setId(id);
        return getMapper().deleteByPrimaryKey(facadeClassKeyDo);
    }

    /**
     * 查询接口类信息
     *
     * @param facadeClass 条件
     * @return List<FacadeClassDo>
     */
    @Override
    public List<FacadeClassDo> selectByExample(FacadeClassQuery facadeClass) {
        return getMapper().selectByExample(buildExample(facadeClass));
    }

    /**
     * 新增接口类信息
     *
     * @param facadeClass 条件
     * @return int>
     */
    @Override
    public Long insertBySelective(FacadeClassDo facadeClass) {
        if (facadeClass == null) {
            return -1L;
        }
        if(null == facadeClass.getId()){
            Long id = SequenceTool.nextLongId();
            facadeClass.setId(id);
        }
        getMapper().insertSelective(facadeClass);
        return facadeClass.getId();
    }

    /**
     * 修改接口类信息
     *
     * @param facadeClass
     * @return
     */
    @Override
    public int updateBySelective(FacadeClassDo facadeClass) {
        if (facadeClass == null) {
            return -1;
        }
        return getMapper().updateByPrimaryKeySelective(facadeClass);
    }

    /**
     * 构建接口类Example信息
     *
     * @param facadeClass
     * @return
     */
    public FacadeClassExample buildExample(FacadeClassQuery facadeClass) {
        FacadeClassExample example = new FacadeClassExample();
        FacadeClassExample.Criteria criteria = example.createCriteria();
        if (facadeClass != null) {
            //添加查询条件
            if (null != facadeClass.getId()) {
                criteria.andIdEqualTo(facadeClass.getId());
            }
            if (StringUtil.isNotEmpty(facadeClass.getClassCode())) {
                criteria.andClassCodeEqualTo(facadeClass.getClassCode());
            }
            if (StringUtil.isNotEmpty(facadeClass.getClassName())) {
                criteria.andClassNameEqualTo(facadeClass.getClassName());
            }
            if (null != facadeClass.getProjectId()) {
                criteria.andProjectIdEqualTo(facadeClass.getProjectId());
            }
            if (StringUtil.isNotEmpty(facadeClass.getIsPublic())) {
                criteria.andIsPublicEqualTo(facadeClass.getIsPublic());
            }
            if (StringUtil.isNotEmpty(facadeClass.getFacadeDesc())) {
                criteria.andFacadeDescEqualTo(facadeClass.getFacadeDesc());
            }
            if (StringUtil.isNotEmpty(facadeClass.getFacadeVersion())) {
                criteria.andFacadeVersionEqualTo(facadeClass.getFacadeVersion());
            }
            if (null != facadeClass.getCreateBy()) {
                criteria.andCreateByEqualTo(facadeClass.getCreateBy());
            }
            if (null != facadeClass.getCreateDateBegin()) {
                criteria.andCreateDateGreaterThanOrEqualTo(facadeClass.getCreateDateBegin());
            }
            if (null != facadeClass.getCreateDateEnd()) {
                criteria.andCreateDateLessThanOrEqualTo(facadeClass.getCreateDateEnd());
            }
            if (null != facadeClass.getUpdateBy()) {
                criteria.andUpdateByEqualTo(facadeClass.getUpdateBy());
            }
            if (null != facadeClass.getUpdateDateBegin()) {
                criteria.andUpdateDateGreaterThanOrEqualTo(facadeClass.getUpdateDateBegin());
            }
            if (null != facadeClass.getUpdateDateEnd()) {
                criteria.andUpdateDateLessThanOrEqualTo(facadeClass.getUpdateDateEnd());
            }
        }
        return example;
    }
}
